package org.yjw;

import java.io.*;

public class ByteArrayOutputStreamAndByteArrayInputStreamTest {

    public static void main(String[] args) {
        read();
        write();
    }

    public static void read() {
        String mes = "hello,world";
        byte[] b = mes.getBytes();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b);
        int result = -1;
        while ((result = byteArrayInputStream.read()) != -1) {
            System.out.println((char) result);
        }
        try {
            byteArrayInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write() {
        String mes = "你好,world\r\nYJW";
        byte[] b = mes.getBytes();

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            byteArrayOutputStream.write(b);
            FileOutputStream fileOutputStream = new FileOutputStream(new File("D:/123.txt"));
            byteArrayOutputStream.writeTo(fileOutputStream);
            fileOutputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
